<?php
	namespace app\api\controller;
	use think\Controller;
	use think\facade\Validate;

	/**
	* 
	*/
	class Common extends Controller
	{
		//过滤后的参数
		protected $params;
		//自定义验证规则
		protected $rules = array(
				'User'=>array(
					'login'=>array(
		                'user_name' => 'require',
		                'user_pwd'  => 'require|length:6',
		            ),
				)
			);
		protected function initialize(){
			parent::initialize();
			/*$this->check_time($this->request->only(['time']));
			$this->check_token($this->request->param());*/
			$this->params = $this->check_params($this->request->except(['time','token']));
		}
		/**
		 * 验证时间戳
		 */
		public function check_time($arr){
			if(!isset($arr['time']) || intval($arr['time'])<=1){
				$this->ret_msg(400,'时间戳不正确！');
			}

			if(time()-intval($arr['time'])>60){
				$this->ret_msg(400,'请求超时！');
			}
		}
		/**
		 * 验证TOKEN
		 */
		public function check_token($arr){
			if(empty($arr['token'])){
				$this->ret_msg(400,'token不能为空！');
			}

			$token = $arr['token'];

			unset($arr['token']);
			$service_token = '';

			foreach ($arr as $k => $v) {
				$service_token .= md5($v);
			}
			$service_token = md5('api_'.$service_token.'_api');
			// dump($service_token);die;
			if($token!==$service_token){
				$this->ret_msg(400,'token验证失败！');
			}
		}
		/**
		 * 验证数据
		 */
		public function check_params($arr){
			$rule = $this->rules[$this->request->controller()][$this->request->action()];
			$validate = Validate::make($rule);

			if(!$validate->check($arr)){
				$this->ret_msg(400,$validate->getError());
			}
			return $arr;
			

		}
		public function ret_msg($code,$msg = '',$data = []){

			$ret_data['code'] = $code;
			$ret_data['msg'] = $msg;
			$ret_data['data'] = $data;

			echo json_encode($ret_data);die;
		}

		/**
	     * 检测用户名并返回用户名类别
	     * @param  [string] $username [用户名, 可能是邮箱, 也可能是手机号]
	     * @return [string]           [检测结果]
	     */
	    public function check_username($username) {
	        /*********** 判断是否为邮箱  ***********/
	        $is_email = Validate::isEmail($username) ? 1 : 0;
	        /*********** 判断是否为手机  ***********/
	        $is_phone = preg_match('/^1[34578]\d{9}$/', $username) ? 4 : 2;
	        /*********** 最终结果  ***********/
	        $flag = $is_email + $is_phone;
	        switch ($flag) {
	        /*********** not phone not email  ***********/
	        case 2:
	            $this->ret_msg(400, '邮箱或手机号不正确!');
	            break;
	        /*********** is email not phone  ***********/
	        case 3:
	            return 'email';
	            break;
	        /*********** is phone not email  ***********/
	        case 4:
	            return 'phone';
	            break;
	        }
	    }
	    public function check_exist($value, $type, $exist) {
	        $type_num  = $type == "phone" ? 2 : 4;
	        $flag      = $type_num + $exist;
	        $phone_res = db('user')->where('user_phone', $value)->find();
	        $email_res = db('user')->where('user_email', $value)->find();
	        switch ($flag) {
	        /*********** 2+0 phone need no exist  ***********/
	        case 2:
	            if ($phone_res) {
	                $this->ret_msg(400, '此手机号已被占用!');
	            }
	            break;
	        /*********** 2+1 phone need exist  ***********/
	        case 3:
	            if (!$phone_res) {
	                $this->ret_msg(400, '此手机号不存在!');
	            }
	            break;
	        /*********** 4+0 email need no exist  ***********/
	        case 4:
	            if ($email_res) {
	                $this->ret_msg(400, '此邮箱已被占用!');
	            }
	            break;
	        /*********** 4+1 email need  exist  ***********/
	        case 5:
	            if (!$email_res) {
	                $this->ret_msg(400, '此邮箱不存在!');
	            }
	            break;
	        }
	    }
	}